package com.yvon.maple.service.msg.core.handler.handler;

import com.yvon.boot.mail.core.MailTemplate;
import com.yvon.maple.service.msg.core.common.domain.TaskInfo;
import com.yvon.maple.service.msg.core.common.enums.ChannelType;
import com.yvon.maple.service.msg.core.common.model.content.EmailContentModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : Yvon
 * @since : 2021-12-23
 */
@Slf4j
@Component
public class EmailHandler extends Handler{


    public EmailHandler() {
        channelCode = ChannelType.EMAIL.getCode();
    }

    @Resource
    private MailTemplate mailTemplate;


    @Override
    public void handler(TaskInfo taskInfo) {

        EmailContentModel contentModel = (EmailContentModel) taskInfo.getContentModel();


            taskInfo.getReceiver().forEach(receiver -> {
                try {
                    mailTemplate.sendHtmlMail(receiver, contentModel.getSubject(), contentModel.getContent(), taskInfo.getSendAccount());
                } catch (Exception e) {
                    log.info("邮件发送失败：{}", e.getMessage());
                }
            });
        log.info("邮件发送完毕");
    }
}
